var $http = require('../../../utils/api/index.js')
var $dtdetailhttp = require('../../../utils/api/dtdetail.js')
const $ = require('../../../utils/js.js')
const app = getApp()
var img_gy_src = $.img_gy_src;
var image_gy_src = $.image_gy_src;
Page({
  // 页面的初始数据
  data: {
    img_gy_src: img_gy_src,
    image_gy_src: image_gy_src,
    tablists: [{
      name: '推荐'
    }, {
      name: '关注'
    }, {
      name: '身边'
    }, {
      name: '热帖'
    }],
    currentDt: 'is_recommend',
    dongtailists: [],
    keyword: '',
    pn: 1,
    ding_wei: false
  },
  // 生命周期函数--监听页面加载
  onLoad: function(options) {
    if (getApp().data.hide) {
      var tablists = this.data.tablists;
      tablists.splice(3, 1);
      this.setData({
        tablists: tablists,
      })
    }

    $.ding_wei((data) => {
      console.log(data)
      this.setData({
        ding_wei: data.root
      })
    })
    console.log(options.keyword)
    if (options.catid) {
      this.setData({
        catid: options.catid,
      })
    }
    if (options.keyword) {
      this.setData({
        keyword: options.keyword ? options.keyword : ''
      })
      // 动态设置标题
      $.title(options.keyword);
    }
    if (options.title) {
      // 动态设置标题
      $.title(options.title);
    }
    this.xuanran(this.data.currentDt)
  },
  // 生命周期函数--监听页面初次渲染完成
  onReady: function() {

  },
  // 生命周期函数--监听页面显示
  onShow: function() {

  },
  // 自定义函数   开始
  // 跳转到名片详情
  go_name_card(e) {
    var user_id = $.data(e, "user_id");
    $.open('/pages/index/name_card_xq/name_card_xq?back=1&id=' + user_id);
  },
  // 关注
  guanzhu(e) {
    var id = e.currentTarget.dataset.id
    var is_care = e.currentTarget.dataset.is_care
    var idx = e.currentTarget.dataset.index
    console.log(idx)
    $http.care(id, is_care == 0 ? 1 : 2).then((res) => {
      console.log(res)
      wx.showToast({
        title: res.data.msg,
        icon: 'none'
      })
      if (res.data.code == 1) {
        var a = 'guanzhulists[' + idx + '].is_care'
        this.setData({
          [a]: is_care == 0 ? 1 : 0
        })
      }
    })
  },
  xuanran(ctype) {
    this.setData({
      pn: 1
    })
    $http.list(this.data.pn, ctype, app.globalData.longitude, app.globalData.latitude, this.data.catid, this.data.keyword).then((res) => {
      console.log(res);
      if (res.data.code == 1) {
        if (this.data.currentDt == 'care') {
          if (res.data.data.no_care == 1) {
            console.log(this.data.pn);
            $http.circle_users(this.data.pn).then((res1) => {
              this.setData({
                isgzlist: true,
                guanzhulists: res1.data.data,
                loadshow: false
              })
            })
          } else {
            this.setData({
              isgzlist: false,
              dongtailists: res.data.data
            })
          }
        } else {
          this.setData({
            isgzlist: false,
            dongtailists: res.data.data
          })
        }
      }
    })
  },
  tabclick(e) {
    var ctype = ''
    switch (e.detail.index) {
      case 0:
        ctype = 'is_recommend'
        break;
      case 1:
        ctype = 'care'
        break;
      case 2:
        ctype = 'near'
        break;
      case 3:
        ctype = 'hot'
        break;
    }
    this.setData({
      currentDt: ctype
    })
    this.xuanran(ctype)
  },
  dtdetail(e) {
    wx.navigateTo({
      url: '/pages/index/dtdetail/dtdetail?id=' + e.currentTarget.dataset.id
    })
  },
  // 进入图片预览
  yu_lan_fun(e) {
    var img = $.data(e, "img");
    var imgs = $.data(e, "imgs");
    $.yu_lan(img, imgs)
  },
  // 点赞
  dianzan(e) {
    var index = e.currentTarget.dataset.idx;
    var dtid = e.currentTarget.dataset.id;
    var islike = e.currentTarget.dataset.islike;
    const dianzanafter = 'dongtailists[' + index + '].is_like'

    if (islike == 0) {
      // 点赞
      $dtdetailhttp.like(dtid, 1).then((res) => {
        this.setData({
          [dianzanafter]: 1,
        })
      })
    } else {
      // 取消
      $dtdetailhttp.like(dtid, 2).then((res) => {
        this.setData({
          [dianzanafter]: 0,
        })
      })
    }
  },
  //前往聊天
  liao_tian(e) {
    $.deng_lu(function() {
      var receive_id = $.data(e, "receive_id");
      var title = $.data(e, "title");
      $.open("/pages/my/liao_tian_content/liao_tian_content?user_id=" + getApp().globalData.userinfo.id + '&receive_id=' + receive_id + "&title=" + title);
    })
  },
  // 自定义函数   结束
  // 生命周期函数--监听页面隐藏
  onHide: function() {

  },
  // 生命周期函数--监听页面卸载
  onUnload: function() {

  },
  // 页面相关事件处理函数--监听用户下拉动作
  onPullDownRefresh: function() {

  },
  // 页面上拉触底事件的处理函数
  onReachBottom: function() {
    this.setData({
      pn: ++this.data.pn,
      loadshow: true
    })
    $http.list(this.data.pn, this.data.currentDt, app.globalData.longitude, app.globalData.latitude, this.data.catid, this.data.keyword).then((res) => {
      if (res.data.code == 1) {
        if (this.data.currentDt == 'care') {
          if (res.data.data.no_care == 1) {
            $http.circle_users(this.data.pn).then((res1) => {
              this.data.guanzhulists.push(...res1.data.data)
              this.setData({
                isgzlist: true,
                guanzhulists: this.data.guanzhulists,
                loadshow: false
              })
            })
          } else {
            this.setData({
              loadshow: false
            })
          }
        } else {
          setTimeout(() => {
            this.data.dongtailists.push(...res.data.data)
            this.setData({
              isgzlist: false,
              dongtailists: this.data.dongtailists
            })
            this.setData({
              loadshow: false
            })
          }, 1000)
        }
      }
    })
  },
  // 转发
  onShareAppMessage: function(e) {
    if (e.from == "button") {
      var item = e.target.dataset.item;
      return {
        title: item.content,
        path: "/pages/index/dtdetail/dtdetail?notice=1&id=" + item.id
      }
    }
  }
})